from sqlalchemy import Boolean, Column, DateTime, Float, Integer, String, Text, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.db.database import Base

class Campsite(Base):
    """营地模型"""
    __tablename__ = "campsites"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), index=True)
    description = Column(Text)
    address = Column(String(255))
    latitude = Column(Float)
    longitude = Column(Float)
    cover_image = Column(String(255))
    phone = Column(String(20), nullable=True)
    website = Column(String(255), nullable=True)
    business_hours = Column(String(255), nullable=True)
    price_range = Column(String(50), nullable=True)
    facilities = Column(Text, nullable=True)  # JSON格式的设施列表
    is_verified = Column(Boolean, default=False)
    is_active = Column(Boolean, default=True)
    is_deleted = Column(Boolean, default=False)  # 软删除标记
    
    # 关联商家ID
    merchant_id = Column(Integer, ForeignKey("merchants.id"), nullable=True)
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    # 关系
    posts = relationship("Post", back_populates="campsite")
    images = relationship("CampsiteImage", back_populates="campsite", cascade="all, delete-orphan")
    followers = relationship("CampsiteFollow", back_populates="campsite")
    merchant = relationship("Merchant", back_populates="campsites")


class CampsiteImage(Base):
    """营地图片模型"""
    __tablename__ = "campsite_images"

    id = Column(Integer, primary_key=True, index=True)
    campsite_id = Column(Integer, ForeignKey("campsites.id"))
    url = Column(String(255))
    description = Column(String(255), nullable=True)
    order = Column(Integer, default=0)
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # 关系
    campsite = relationship("Campsite", back_populates="images")


class CampsiteFollow(Base):
    """营地关注关系模型"""
    __tablename__ = "campsite_follows"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    campsite_id = Column(Integer, ForeignKey("campsites.id"))
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # 关系
    user = relationship("User", back_populates="followed_campsites")
    campsite = relationship("Campsite", back_populates="followers") 